/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.inmp.conf;

import java.io.ByteArrayInputStream;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.sinodata.bsm.common.utils.DateUtil;
import com.sinodata.bsm.common.vo.Event;
import com.sinodata.bsm.common.vo.Res;
import com.sinodata.bsm.inmp.cache.CacheFactory;
import com.sinodata.bsm.inmp.cache.ResCache;
import com.sinodata.bsm.inmp.message.Messenger;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-11-29 下午9:58:48          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class EventDispatcher implements Runnable {

    private static final Logger logger = Logger.getLogger(EventDispatcher.class);

    private static final String SYS_LOG = "SYSLOG";

    private static final String TRAP = "TRAP";

    private ResCache resCache = (ResCache) CacheFactory.getCache(ResCache.class);

    private Object obj;

    public EventDispatcher(Object obj) {
        this.obj = obj;
    }

    @SuppressWarnings("unchecked")
    @Override
    public void run() {
        try {
            SAXReader reader = new SAXReader();
            String v = obj.toString();
            //tdd
            logger.info("接收到事件");

            Document doc = reader.read(new ByteArrayInputStream(v.getBytes("UTF-8")));
            if (doc == null) {
                return;
            }
            System.out.println(v);
            List<Element> elements = doc.selectNodes("/ITSM/EVENT");
            for (Element e : elements) {
                String orginal = e.attributeValue("ORGINAL").trim().toUpperCase();
                if (SYS_LOG.equals(orginal) || TRAP.equals(orginal)) {//只取syslog trap 事件 
                    Event event = new Event();
                    Res res = resCache.getResIdByIId(e.attributeValue("JOB_ID").trim() + "," + e.attributeValue("NODE_ID"));
                    if (res == null) {
                        return;
                    }
                    event.setResId(res.getId());
                    //                    event.setResId(3193L);
                    event.setEventTypeId(ConfManager.getInstance().getEventTypeMap().get(orginal)); //ORGINAL
                    //                    event.setStatus(Integer.parseInt(e.attributeValue("STATUS").trim()));
                    event.setStatus(0);
                    //                    event.setLevel(Integer.parseInt(e.attributeValue("SEVERITY").trim()));
                    event.setLevel(2);
                    Date d = DateUtil.parseDate(e.attributeValue("CREATE_TIME").trim(), "yyyy-MM-dd HH:mm:ss");
                    event.setOccurTime(d);
                    event.setLastOccurTime(d);

                    event.setCatalog(1);
                    String des = e.attributeValue("DESCRIPTION");
                    if (des.length() > 1024) {
                        des = des.substring(0, 1024);
                    }
                    event.setMsg(des);

                    System.out.println("将事件放入队列中..." + event.toString());
                    Messenger.getInstance().sendEvent(event);
                } else {
                    continue;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 转为对应的事件级别
     * @param level
     * @return
     */
    private String convertLevel(String level) {

        return "";
    }

    /**
     * 转为对应的事件状态
     * @param status
     * @return
     */
    private String convertStatus(String status) {
        return "";
    }

}
